package com.example.day3_notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.bt_normal).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                normal();

            }
        });
        findViewById(R.id.bt_group).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                group();

            }
        });
        findViewById(R.id.bt_progress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progress();
            }
        });
        findViewById(R.id.bt_customer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customer();
            }
        });

    }
    // 自定义通知
    private void customer() {
        //TODO 1：获得通知管理器
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //TODO 2：创建自定义布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.music);
        //为布局设置内容
        remoteViews.setTextViewText(R.id.name,"双节棍");
        remoteViews.setImageViewResource(R.id.pre,R.drawable.syg);
        remoteViews.setImageViewResource(R.id.next,R.drawable.xyg);
        remoteViews.setImageViewResource(R.id.play,R.drawable.bf);
        remoteViews.setImageViewResource(R.id.iv,R.drawable.wl);


        //TODO 3：创建通知并设置自定义布局
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.car)
                .setCustomContentView(remoteViews)//自定义布局
                .build();
        //TODO 4：发送通知
        manager.notify(1,notification);


    }

    private int progress = 0;
    private void progress() {
        //TODO 1：获得通知管理器
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //TODO 2：创建通知
        final Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.drawable.car);
        Notification notification = builder.build();
        //TODO 4：定时器模拟下载安装过程
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //模拟下载进度   //参数一 最大值 参数二 当前进度  参数三 是否模糊
                builder.setProgress(100,progress,false);//明确进度条
                builder.setContentText("正在下载"+progress+"/100");
                Notification notification1 = builder.build();
                manager.notify(1,notification1);
                progress+=20;
                //如果下载成功 正在安装过程
                if(progress > 100){
                    builder.setProgress(100,0,true);//模糊进度条
                    builder.setContentText("正在安装----");
                    Notification notification2 = builder.build();
                    manager.notify(1,notification2);
                    try {
                        Thread.sleep(5000);//模拟按章时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    timer.cancel();//取消定时器
                    manager.cancel(1);//关闭通知

                }
            }
        },0,1000);
        //TODO 3：发送通知
        manager.notify(1,notification);

    }

    private void group() {
       //TODO 1：管理器
       NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       //TODO 2:通知
        NotificationCompat.Builder builder0 = new NotificationCompat.Builder(this);
        NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this);
        NotificationCompat.Builder builder2 = new NotificationCompat.Builder(this);

        Notification notification0 = builder0.setSmallIcon(R.drawable.car)
                .setContentTitle("标题111")
                .setContentText("内容111")
                .setGroupSummary(true)
                .setGroup("微信")
                .build();
        Notification notification1 = builder1.setSmallIcon(R.drawable.car)
                .setContentTitle("标题222")
                .setContentText("内容222")
                .setGroup("微信")
                .build();
        Notification notification2 = builder2.setSmallIcon(R.drawable.car)
                .setContentTitle("标题333")
                .setContentText("内容333")
                .setGroup("微信")
                .build();

        //TODO 3:发送通知
        manager.notify(1,notification0);
        manager.notify(2,notification1);
        manager.notify(3,notification2);





    }

    private void normal() {
        //TODO 1：获得通知管理器
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //TODO 2：创建通知对象
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.drawable.car)//必须设置小图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.shouye))
                .setContentTitle("表题")
                .setContentText("内容")
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .build();
        //TODO 3：通知管理器发送通知
        manager.notify(1,notification);
    }
}
